#!/usr/bin/env bash
#
# Copyright 2018, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in
#       the documentation and/or other materials provided with the
#       distribution.
#
#     * Neither the name of the copyright holder nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# pmempool_sync_remote/TEST28 -- pmempool sync with remote replication
#                                and bad block recovery files:
#                                - the pool consists of 3 parts
#                                - bad block in the 2nd part: offset: 0 length: 8*512
#                                (it is similar to TEST24)
#

# standard unit test setup
. ../unittest/unittest.sh

require_test_type medium
require_fs_type any
require_build_type debug nondebug
require_linked_with_ndctl $PMEMPOOL$EXESUFFIX

require_nodes 2

setup

require_node_libfabric 0 $RPMEM_PROVIDER
require_node_libfabric 1 $RPMEM_PROVIDER

init_rpmem_on_node 0 1

require_node_log_files 0 pmemobj$UNITTEST_NUM.log
require_node_log_files 0 pmempool$UNITTEST_NUM.log

LAYOUT="layout"
LOG=out${UNITTEST_NUM}.log
rm -f $LOG && touch $LOG

POOLSET_LOCAL="local_pool.set"
POOLSET_REMOTE="remote_pool.set"
RECOVERY_FILE_P0=$DIR/${POOLSET_LOCAL}_r0_p0_badblocks.txt
RECOVERY_FILE_P1=$DIR/${POOLSET_LOCAL}_r0_p1_badblocks.txt
RECOVERY_FILE_P2=$DIR/${POOLSET_LOCAL}_r0_p2_badblocks.txt

create_poolset $DIR/$POOLSET_LOCAL \
	8M:${NODE_DIR[0]}/pool.local.part.0:x \
	8M:${NODE_DIR[0]}/pool.local.part.1:x \
	8M:${NODE_DIR[0]}/pool.local.part.2:x \
	M \
	${NODE_ADDR[1]}:$POOLSET_REMOTE

create_poolset $DIR/$POOLSET_REMOTE \
	24M:${NODE_DIR[1]}/pool.remote:x

# create the recovery files
create_recovery_file $RECOVERY_FILE_P0
create_recovery_file $RECOVERY_FILE_P1 0 8
create_recovery_file $RECOVERY_FILE_P2

copy_files_to_node 0 ${NODE_DIR[0]} $DIR/$POOLSET_LOCAL
copy_files_to_node 1 ${NODE_DIR[1]} $DIR/$POOLSET_REMOTE

expect_normal_exit run_on_node 0 ../pmempool rm -sf ${NODE_DIR[0]}$POOLSET_LOCAL
expect_normal_exit run_on_node 0 "../obj_verify ${NODE_DIR[0]}$POOLSET_LOCAL $LAYOUT c v &>> $LOG"

###############################################################################
# Enabling features in poolsets with remote replicas is not supported now,
# so we have to use the following workaround to enable
# the POOL_FEAT_CHECK_BAD_BLOCKS compat feature in such poolset.
# We create the auxiliary poolset without the remote replica
# and enable this compat feature on all nodes separately.

create_poolset $DIR/${POOLSET_LOCAL}_aux \
	8M:${NODE_DIR[0]}/pool.local.part.0:x \
	8M:${NODE_DIR[0]}/pool.local.part.1:x \
	8M:${NODE_DIR[0]}/pool.local.part.2:x

copy_files_to_node 0 ${NODE_DIR[0]} $DIR/${POOLSET_LOCAL}_aux

turn_on_checking_bad_blocks_node 0 ${NODE_DIR[0]}/${POOLSET_LOCAL}_aux
turn_on_checking_bad_blocks_node 1 ${NODE_DIR[1]}/${POOLSET_REMOTE}

###############################################################################

# inject bad block:
expect_normal_exit run_on_node 0 "dd if=/dev/zero of=${NODE_DIR[0]}/pool.local.part.1 bs=512 seek=0 count=8 conv=notrunc status=none"

# copy the recovery files
copy_files_to_node 0 ${NODE_DIR[0]} $RECOVERY_FILE_P0 $RECOVERY_FILE_P1 $RECOVERY_FILE_P2

expect_abnormal_exit run_on_node 0 "../obj_verify ${NODE_DIR[0]}$POOLSET_LOCAL $LAYOUT v &>> $LOG"
expect_normal_exit run_on_node 0 "../pmempool sync -b -v ${NODE_DIR[0]}$POOLSET_LOCAL &>> $LOG"
expect_normal_exit run_on_node 0 "../obj_verify ${NODE_DIR[0]}$POOLSET_LOCAL $LAYOUT v &>> $LOG"

pass
